package crond

import (
	"context"
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service"
	"sale-admin/internal/dal"
	"sale-admin/internal/dao"
	"sale-admin/internal/models"

	"github.com/golang-module/carbon"
)

// 更新商品运营日报订单信息 [统计前一个小时]
func SaveGoodsReportOrder() error {
	db := mysql.NewDB()
	// 查询前一小时的订单
	date := carbon.Now().SubHour().ToDateString()
	recentHour := carbon.Now().SubHour().Format("Y-m-d H") // 2024-04-15 07
	var goodsIDs []string
	db.Model(&models.MaOrder{}).
		Where("created_date = ?", date).
		Where("add_time like ?", recentHour+"%").
		Pluck("goods_id", &goodsIDs)
	detailMap := dao.Goods{}.DetailMap(context.Background(), dal.Q, goodsIDs)
	for _, v := range detailMap {
		service.GoodsDailyReport{}.UpdateOrder(context.Background(), db, v.GoodsID, v.GoodsName1, date)
	}
	return nil
}
